// Models/users.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var md5 = require('md5');

// 声明一个数据集 对象
var userSchema = new Schema({
    username: {
        type: String,
        unique: true
    },
    password: {
        type: String
    },
    filePath:{
        type: String,        
    },
    // 权限管理
    // 0: nomal user
    // 1: verified user
    // 2: professonal user
    // ...
    // >100: admin
    // >500: super admin
    role: {
        type: Number,
        default: 100
    },
    createAt: {
        type: Date,
        default: Date.now()
    }
});
userSchema.methods = {
    comparePassword: function (_password, cb) {
        console.log(_password)
        console.log(this.password)
        if (_password == this.password) {
            cb(null, {
                role: this.role,
                username: this.username,
                userId: this._id
            })
        } else {
            cb({ msg: '密码不匹配' }, false)
        }
    }
}
// 将数据模型暴露出去
module.exports = mongoose.model('users', userSchema);